#include "pcrTable.h"



PCRTable::PCRTable(QStringList header, QWidget* parent)
{


    this->setParent(parent);
    this->setMinimumWidth(200);
    this->setMinimumHeight(200);

    _layout = new QVBoxLayout(this);
    _table = new QTableWidget(this);

    this->setLayout(_layout);
    _layout->addWidget(_table);
    _table->setColumnCount(header.size());

    _table->setHorizontalHeaderLabels(header);    // 设置表头
    _table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(255, 255, 255);}");
    _table->setEditTriggers(QAbstractItemView::NoEditTriggers);   // 设置每一行不可编辑
    _table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    _table->show();

    _table->installEventFilter(this);
    _table->verticalHeader()->setHidden(true);


    connect(_table, SIGNAL(itemSelectionChanged()), this, SLOT(on_selectionChanged()));


#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 16);
            _table->setFont(font);
            _table->horizontalHeader()->setFont(font);
        }
    }
#endif

}

// 加载时间：读取了数据，但是还没进行计算处理（？
void PCRTable::init(PCRState* state){

    try {

        pState = state;
        state->_table = this->_table;

#ifdef Q_OS_ANDROID
        // 我也不知道排序到安卓端为啥就不好使了……
        QVector<PosiName> posiList;
        for(char posi1 = 'A'; posi1 <= 'H'; posi1 ++){
            foreach(QString posi, pState->posiList){
                if(posi.contains(posi1) && posi.size() == 2){
                    posiList.append(posi);
                }
            }
            foreach(QString posi, pState->posiList){
                if(posi.contains(posi1) && posi.size() == 3){
                    posiList.append(posi);
                }
            }
        }
        pState->posiList.clear(); pState->posiList.swap(posiList);

#endif

        QVector<QVector<QString>> data;
        foreach(PosiName posi, pState->posiList){
            foreach(ChannelName channel, pState->channelList){
                data.append(QVector<QString>());
                data.back().append(pState->sampleInfo_posi[posi][0]);    // 孔位
                data.back().append(pState->sampleInfo_posi[posi][1]);    // 样本名称
                data.back().append(pState->sampleInfo_posi[posi][2]);    // 样本类型
                data.back().append(channel);                             // 荧光通道
                data.back().append(0);
                // 浓度
                if(pState->sampleInfo_posi[posi][2].contains("标准")){
                    data.back().append(pState->sampleInfo_posi[posi][3]);         // 浓度
                    data.back().append(pState->sampleInfo_posi[posi][3]);         // 浓度均值
                }else{
                    data.back().append(0);         // 浓度
                    data.back().append(0);         // 浓度均值
                }
            }
        }

        _table->clearContents();
        _table->setRowCount(data.size());
        for(int r = 0; r < data.size(); r ++){
            for(int c = 0; c < 7; c ++){
                _table->setItem(r, c, new QTableWidgetItem(data[r][c]));
            }
        }
        _table->show();
        //pState->emitFilterChanged();
    } catch (...) {

        qDebug() << __FUNCTION__ << "发生异常";
    }

}


QTableWidget* PCRTable::getTable(){
    return _table;
}


void PCRTable::on_filterChanged(){
    try {
        QStringList res;
        for(int r = 0; r < _table->rowCount(); r ++){
            if(pState->posiList_filter.contains(_table->item(r, 0)->text()) &&
                    pState->nameList_filter.contains(_table->item(r, 1)->text()) &&
                    pState->typeList_filter.contains(_table->item(r, 2)->text()) &&
                    pState->channelList_filter.contains(_table->item(r, 3)->text()) &&
                    pState->curveList.contains(_table->item(r, 3)->text() + "_" + _table->item(r, 0)->text())){
                res.append(_table->item(r, 3)->text() + "_" + _table->item(r, 0)->text());
                _table->showRow(r);
            }else{
                _table->hideRow(r);
            }
        }
        pState->filterResult = res;

        pState->emitFilterResultChanged();
    } catch (...) {

        qDebug() << __FUNCTION__ << "发生异常";
    }

}


void PCRTable::on_modelChanged(){


    // 更新阈值线
    for(int r = 0; r < _table->rowCount(); r ++){
        QString posi = _table->item(r, 0)->text();
        QString channel = _table->item(r, 3)->text();
        QString name = channel + '_' + posi;

        _table->item(r, 4)->setText(QString::number(pState->Ct[name], 'f', 2));
        _table->item(r, 5)->setText(QString::number(pState->concList[name], 'f', 6));
        _table->item(r, 6)->setText(QString::number(pState->avgConcList[channel], 'f', 6));

        if(_table->item(r, 4)->text() == "0.00"){
            _table->item(r, 4)->setText(FAILURE_VALUE);
        }if(_table->item(r, 5)->text() == "inf" || pState->concList[name] == 0){
            _table->item(r, 5)->setText(FAILURE_VALUE4);
        }if(_table->item(r, 6)->text() == "inf"){
            _table->item(r, 6)->setText(FAILURE_VALUE4);
        }

    }

}

void PCRTable::on_thrChanged(){
    try {
        for(int r = 0; r < _table->rowCount(); r ++){
            QString posi = _table->item(r, 0)->text();
            QString channel = _table->item(r, 3)->text();
            QString name = channel + '_' + posi;

            _table->item(r, 4)->setText(QString::number(pState->Ct[name], 'f', 2));

        }
    } catch (...) {
        qDebug() << __FUNCTION__ << "发生异常";
        return;
    }

}

void PCRTable::on_pageChanged(){

    if(pState->curPage == PCRState::RAW){
        _table->setColumnHidden(4, true);
        _table->setColumnHidden(5, true);
        _table->setColumnHidden(6, true);
    }else if(pState->curPage == PCRState::STANDARD){
        _table->setColumnHidden(4, false);
        _table->setColumnHidden(5, false);
        _table->setColumnHidden(6, false);
    }else{
        _table->setColumnHidden(4, false);
        _table->setColumnHidden(5, true);
        _table->setColumnHidden(6, true);
    }
}

void PCRTable::on_selectionChanged(){


    pState->boldResult.clear();

    QList<QTableWidgetSelectionRange> rangeList = _table->selectedRanges();
    foreach(QTableWidgetSelectionRange range, rangeList){
        for(int r = range.topRow(); r <= range.bottomRow(); r ++){
            QString name = _table->item(r, 3)->text() + '_' + _table->item(r, 0)->text();
            if(pState->filterResult.contains(name)){
                pState->boldResult.append(name);
            }
        }
    }

    pState->emitBoldResultChanged();

}

void PCRTable::on_standardChanged(){
    try {
        // 更新阈值线
        for(int r = 0; r < _table->rowCount(); r ++){
            QString posi = _table->item(r, 0)->text();
            QString channel = _table->item(r, 3)->text();
            QString name = channel + '_' + posi;

            _table->item(r, 5)->setText(QString::number(pState->concList[name], 'f', 6));
            _table->item(r, 6)->setText(QString::number(pState->avgConcList[channel], 'f', 6));

            if(_table->item(r, 5)->text() == "inf" || pState->concList[name] == 0){
                _table->item(r, 5)->setText(FAILURE_VALUE4);
            }if(_table->item(r, 6)->text() == "inf"){
                _table->item(r, 6)->setText(FAILURE_VALUE4);
            }

        }
    } catch (...) {
        qDebug() << __FUNCTION__ << "发生异常";
        return;
    }

}
